/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package mnstudent.dataacesslayer;

import mnstudent.datatranferobject.PositionDTO;
import mnstudent.datatranferobject.PositionDTO;
import java.sql.ResultSet;

/**
 *
 * @author 11520_000
 */
public class PositionDAL {
     
    //<editor-fold defaultstate="collapsed" desc="Attributes">
    private PositionDTO m_PositionDTO;
    private DataExecuteUseStoreProcedure m_DataExecute;
    private String  m_sqlCmd;
    
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Constuctor">
    public PositionDAL(){
        this.m_PositionDTO = new PositionDTO();
        this.m_DataExecute = new DataExecuteUseStoreProcedure();    
        this.m_sqlCmd = "";
    }
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="Method">
    
     /**
     * Ham lay tat ca cac Nhan vien
     *
     * @return ResultSet
     */
    public ResultSet getAllPosition(){
        try
        {
            m_sqlCmd = "{Call GetAllPositionFromDatabase}";
            ResultSet _rsData = this.m_DataExecute.getDataFromDatabase(m_sqlCmd,null);
            return _rsData;
        }catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return null;
    }
    
     public ResultSet getAllIdAndNameOfPositionFromDatabase()
    {
       try {
            m_sqlCmd = "{Call GetAllIdAndNameOfPositionFromDatabase}";
            return this.m_DataExecute.getDataFromDatabase(m_sqlCmd, null);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return null; 
    }
    
    /**
     * Ham lay khoa theo ma khoa
     * @param _id
     * @return tra ve table chon
     */
    public ResultSet getPositionById(String _id)
    {
        try {
            m_sqlCmd = "{Call GetPositionByIdFromDatabase(?)}";
            String[] arrId = {_id};
            return this.m_DataExecute.getDataFromDatabase(m_sqlCmd, arrId);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return null;
    }

    public ResultSet getIdAndNameOfPositionById(String _id)
    {
         try {
            m_sqlCmd = "{Call GetIdAndNameOfPositionByIdFromDatabase(?)}";
            String[] arrId = {_id};
            return this.m_DataExecute.getDataFromDatabase(m_sqlCmd, arrId);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return null;
    }
     /**
     * ham them moi khoa xuong database
     * @param _position
     * @return true: success, false: fail
     */
    public boolean insertPositionToDatabase(PositionDTO _position)
    {
       try {
           m_sqlCmd = "{Call InsertPositionToDatabase(?,?,?,?,?,?,?,?,?,?,?,?)}";
           Object[] arrStr = {_position.getPositionID(), _position.getNameOfPosition(), 
               _position.isSettingSystem(), _position.isRegulatoryChange(),
           _position.isStudentManager(), _position.isCreateCourseRegistration(),
           _position.isCreateReceipt(), _position.isSlitClass(), 
           _position.isSearchStudent(), _position.isSearchReceipt(),
           _position.isSearchSubject(), _position.isCreateReport()};

           return this.m_DataExecute.updateDataToDatabase(m_sqlCmd,arrStr)>0;
           
       } catch (Exception ex) {
          ex.printStackTrace();
       }
        return false;
    }
    
    /**
     * Ham cap nhat sua doi thong tin khoa
     * @param _position
     * @return tue: thanh cong, false: that bai
     */
    public boolean updatePositionToDatabase(PositionDTO _position)
    {
       try {
           m_sqlCmd = "{Call UpdatePositionToDatabase(?,?,?,?,?,?,?,?,?,?,?,?)}";
           Object[] arrStr = {_position.getPositionID(), _position.getNameOfPosition(), 
               _position.isSettingSystem(), _position.isRegulatoryChange(),
           _position.isStudentManager(), _position.isCreateCourseRegistration(),
           _position.isCreateReceipt(), _position.isSlitClass(), 
           _position.isSearchStudent(), _position.isSearchReceipt(),
           _position.isSearchSubject(), _position.isCreateReport()};

           return this.m_DataExecute.updateDataToDatabase(m_sqlCmd,arrStr)>0;
           
       } catch (Exception ex) {
          ex.printStackTrace();
       }
        return false;
    }
     
    /**
     * Ham xoa mot khoa ra khoi database
     * @param _position
     * @return 
     */
     public boolean deletePositionFromDatabase(PositionDTO _position)
    {
        try {
           m_sqlCmd = "{Call DeletePositionFromDatabase(?)}";
           String[] arrStr = {_position.getPositionID()};

           return this.m_DataExecute.updateDataToDatabase(m_sqlCmd, arrStr)>0;
           
       } catch (Exception ex) {
          ex.printStackTrace();
       }
        return false;
    }
    //</editor-fold>
}
